﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace WcfClientGenerator.Model.Common
{
    /// <summary>
    /// Serializes and deserializes an object, in xml format.
    /// </summary>
    public class SerializationManager
    {
        /// <summary>Deserializes the XML content of a file into an object graph.</summary>
        /// <param name="path">The file path to deserialize the object.</param>
        /// <param name="type">The type of the object to deserialize.</param>
        /// <returns>The deserialized object.</returns>	
        public static object XmlDeserialize(string path, Type type)
        {

            FileStream fileStream = null;
            try
            {
                if (File.Exists(path))
                {
                    fileStream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                    if (fileStream.Length != 0)
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(type);
                        object o = xmlSerializer.Deserialize(fileStream);
                        return o;
                    }
                    else
                        return null;
                }
                else
                    return null;
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }

        /// <summary>Serializes in XML format an object, or graph of connected objects.</summary>
        /// <param name="path">The file path to serialize the object.</param>
        /// <param name="value">The object to serialize .</param>
        public static void XmlSerialize(string path, object value)
        {
            System.IO.FileStream fileStream = null;
            try
            {
                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);

                fileStream = System.IO.File.Create(path);

                XmlSerializer xmlSerializer = new XmlSerializer(value.GetType());
                xmlSerializer.Serialize(fileStream, value);
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }
        }

        /// <summary>Serializes in XML format an object, or graph of connected objects.</summary>
        /// <param name="value">The object to serialize .</param>
        public static string XmlSerializeString(object value)
        {
            System.IO.StringWriter writer = null;
            try
            {
                writer = new StringWriter();
                XmlSerializer xmlSerializer = new XmlSerializer(value.GetType());
                xmlSerializer.Serialize(writer, value);

                return writer.ToString();
            }
            finally
            {
                if (writer != null)
                    writer.Close();
            }
        }

        /// <summary>Deserializes the XML content of a file into an object graph.</summary>
        /// <param name="path">The file path to deserialize the object.</param>
        /// <param name="type">The type of the object to deserialize.</param>
        /// <returns>The deserialized object.</returns>	
        public static object XmlDeserializeString(string xml, Type type)
        {
            System.IO.StringReader reader = null;
            try
            {
                reader = new StringReader(xml);
                if (xml.Length != 0)
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(type);
                    return xmlSerializer.Deserialize(reader);
                }
                else
                    return null;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }
    }
}
